import Vue from 'vue'
import VueRouter from 'vue-router'
import mine from './mine'
import company from './company'
import brushquestions from './brushquestions'
import recommend from './recommend'
import { getToken } from '@/utils/identification'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login')
  },
  {
    path: '/main',
    redirect: '/main/company',
    name: 'main',
    component: () => import('@/views/main'),
    children: [
      mine,
      company,
      brushquestions,
      recommend
      // 将下面这些拆分成为模块，负责该模块的自己添加路由
      // {
      //   path: 'mine',
      //   name: 'mine',
      //   component: () => import('@/views/mine')
      // },
      // {
      //   path: 'company',
      //   name: 'company',
      //   component: () => import('@/views/company')
      // },
      // {
      //   path: 'brushquestions',
      //   name: 'brushquestions',
      //   component: () => import('@/views/brushquestions')
      // },
      // {
      //   path: 'recommend',
      //   name: 'recommend',
      //   component: () => import('@/views/recommend')
      // }
    ]
  },
  { path: '*', redirect: '/404' },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/404')
  }
]

const router = new VueRouter({
  routes
})
const whiteList = ['/login', '/main', '/ recommend']
router.beforeEach((to, from, next) => {
  // 白名单里面找不到，则需要进行权限校验
  if (!whiteList.includes(to.path)) {
    if (getToken()) {
      next()
      // 有token
    } else {
      next({ path: '/login', query: { redirect: to.path } })
      // 没有token
    }
  } else {
    console.log('white')
    next()
    // 白名单里可以找到
  }
})

export default router
